package com.gskj.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gskj.commonutils.ResultJson;
import com.gskj.educms.entity.CrmBanner;
import com.gskj.educms.service.ICrmBannerService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author Connor-G
 * @since 2020-05-20
 * <p>
 * 后台调用接口
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private ICrmBannerService iCrmBannerService;


    /***
     *  分页查询 banner列表
     * @param page
     * @param size
     * @return
     */
    @GetMapping("page/banner/{page}/{size}")
    public ResultJson getPageBannerList(
            @PathVariable("page") int page,
            @PathVariable("size") int size) {

        // 创建 分页对象
        Page<CrmBanner> crmBannerPage = new Page<>(page, size);

        // 执行查询
        iCrmBannerService.page(crmBannerPage, null);

        List<CrmBanner> records = crmBannerPage.getRecords();
        long total = crmBannerPage.getTotal();

        Map<String, Object> map = new HashMap<>();
        map.put("bannerPage", records);
        map.put("total", total);

        return ResultJson.success().data(map);
    }

    /***
     *  添加 banner
     * @param crmBanner
     * @return
     */
    @PostMapping("/save")
    public ResultJson addBanner(CrmBanner crmBanner) {
        iCrmBannerService.save(crmBanner);

        return ResultJson.success();
    }


    /***
     *  根据 id 删除 banner
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public ResultJson removeBanner(@PathVariable("id") Integer id) {
        iCrmBannerService.removeById(id);
        return ResultJson.success();
    }

    /***
     *  根据id 修改 banner
     * @param crmBanner
     * @return
     */
    @PostMapping("/update")
    public ResultJson editBanner(CrmBanner crmBanner) {
        iCrmBannerService.updateById(crmBanner);
        return ResultJson.success();
    }

    /***
     *  根据 id 查询banner
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public ResultJson getBannerById(@PathVariable("id") Integer id){
        CrmBanner byId = iCrmBannerService.getById(id);

        return ResultJson.success().data("banner", byId);
    }
}

